package org.jeecgframework.web.material.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.material.entity.ProductInputEntity;
import org.jeecgframework.web.material.service.ProductInputServiceI;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



/**   
 * @Description: 产品入库
 * @date 2015-08-19 09:51:09
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/productInputController")
public class ProductInputController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ProductInputController.class);

	@Autowired
	private ProductInputServiceI productInputService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "productInput")
	public ModelAndView productInput(HttpServletRequest request) {
		return new ModelAndView("material/productInput/productInputList");
	}
	
	/**
	 * 产品入库新增页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(ProductInputEntity productInput, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(productInput.getId())) {
			productInput = productInputService.getEntity(ProductInputEntity.class, productInput.getId());
			req.setAttribute("productInputPage", productInput);
		}
		return new ModelAndView("material/productInput/productInput");
	}
	
	/**
	 * 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(ProductInputEntity productInput, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(productInput.getId())) {
			productInput = productInputService.getEntity(ProductInputEntity.class, productInput.getId());
			req.setAttribute("productInputPage", productInput);
		}
		return new ModelAndView("material/productInput/beforeToStore");
	}
	

	/**
	 * 
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(ProductInputEntity productInput,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(ProductInputEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, productInput, request.getParameterMap());
		try{
		//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.productInputService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * 添加产品入库
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(ProductInputEntity productInput, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "产品入库成功";
		try{
			productInputService.saveProduct(productInput);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "产品入库失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	

	/**
	 * 删除产品入库
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(ProductInputEntity productInput, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		productInput = systemService.getEntity(ProductInputEntity.class, productInput.getId());
		message = "产品入库删除成功";
		try{
			productInputService.delete(productInput);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "产品入库删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 批量删除产品入库
	 * 
	 * @return
	 */
	 @RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids,HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		message = "产品入库删除成功";
		try{
			for(String id:ids.split(",")){
				ProductInputEntity productInput = systemService.getEntity(ProductInputEntity.class, 
				id
				);
				productInputService.delete(productInput);
				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "产品入库删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 预入库到正式入库
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(ProductInputEntity productInput, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "产品入库成功";
		try {
			productInputService.beforeToStore(productInput);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "产品入库失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

}
